package com.ccys.doctor.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.doctor.consult.CreateOrUpdateConsultNoteDto;
import com.ccys.db.entity.TblBooking;
import com.ccys.db.entity.TblBookingConsultNote;
import com.ccys.db.entity.TblDoctor;
import com.ccys.db.mapper.TblBookingConsultNoteMapper;
import com.ccys.db.mapper.TblBookingMapper;
import com.ccys.db.vo.doctor.consultNote.GetConsultNoteVo;
import com.ccys.db.vo.doctor.consultNote.ListFileMakeVo;
import com.ccys.doctor.service.ConsultNoteService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: WhiteCoat
 * @description: 咨询相关接口
 * @author: ZhangJingCan
 * @create: 2024-02-05 15:19
 **/

@Service
public class ConsultNoteServiceImpl implements ConsultNoteService {

    @Resource
    private TblBookingConsultNoteMapper tblBookingConsultNoteMapper;

    @Resource
    private TblBookingMapper tblBookingMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultApp deleteConsultNote(String id) {
        if (ObjectUtil.isEmpty(id)) {
            return ResponseResultApp.error("The id field is required.");
        }
        //根据id查询
        TblBookingConsultNote note = tblBookingConsultNoteMapper.findById(id);
        if (ObjectUtil.isEmpty(note)) {
            return ResponseResultApp.error("No data found");
        }
        //软删除设置为已删除
        note.setDelFlag(1);
        //修改进数据库
        tblBookingConsultNoteMapper.updateByPrimaryKeySelective(note);
        return ResponseResultApp.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultApp createOrUpdateConsultNote(CreateOrUpdateConsultNoteDto dto) {
        //校验其余字段
        if (ObjectUtil.isEmpty(dto.getBooking_id())) {
            return ResponseResultApp.error("The booking_id field is required.");
        }
        if (ObjectUtil.isEmpty(dto.getName())) {
            return ResponseResultApp.error("Please enter name.");
        }
        if (ObjectUtil.isEmpty(dto.getDescription())) {
            return ResponseResultApp.error("Please enter description.");
        }

        //判断是新增还是修改
        if (ObjectUtil.isEmpty(dto.getId())) {
            //新增
            //找到booking数据
            Example bookingExample = new Example(TblBooking.class);
            bookingExample.createCriteria()
                    .andEqualTo("id", dto.getBooking_id())
                    .andEqualTo("delFlag", 0);
            TblBooking booking = tblBookingMapper.selectOneByExample(bookingExample);
            if (ObjectUtil.isEmpty(booking)) {
                return ResponseResultApp.error("Unable to find this booking");
            }

            TblBookingConsultNote note = new TblBookingConsultNote();
            note.setMemId(booking.getMemId());
            note.setName(dto.getName());
            note.setDescription(dto.getDescription());
            note.setBookingId(dto.getBooking_id());
            note.setFileNames(dto.getFile_name_new() != null ? dto.getFile_name_new() : null);
            tblBookingConsultNoteMapper.insertSelective(note);
        } else {
            //修改
            //先查询数据 - 如果不存在则返回错误信息
            Example example = new Example(TblBookingConsultNote.class);
            example.createCriteria()
                    .andEqualTo("id", dto.getId())
                    .andEqualTo("delFlag", 0);
            TblBookingConsultNote note = tblBookingConsultNoteMapper.selectOneByExample(example);
            if (ObjectUtil.isEmpty(note)) {
                return ResponseResultApp.error("No data found");
            }
            note.setName(dto.getName());
            note.setDescription(dto.getDescription());
            note.setFileNames(dto.getFile_name_new() != null ? dto.getFile_name_new() : null);
            //这个方法null值会被更新
            tblBookingConsultNoteMapper.updateByPrimaryKey(note);
        }
        return ResponseResultApp.success();
    }

    @Override
    public ResponseResultApp<GetConsultNoteVo> getConsultNote(String bookingId) {
        if (ObjectUtil.isEmpty(bookingId)) {
            return ResponseResultApp.error("The booking_id field is required.");
        }
        TblDoctor doctor = UserUtil.getCurLoginDoctor();
        GetConsultNoteVo vo = tblBookingConsultNoteMapper.getConsultNote(bookingId, doctor.getId());
        //检查文件名不为空
        if (ObjectUtil.isNotEmpty(vo.getFilenames())) {
            List<ListFileMakeVo> list = new ArrayList<>();
            //切割
            String[] split = vo.getFilenames().split("\\|");
            for (String s : split) {
                ListFileMakeVo makeVo = new ListFileMakeVo();
                makeVo.setName(s);
                //todo 这里需要S3的url地址
                makeVo.setUrl("S3的地址/" + s);
                list.add(makeVo);
            }
            vo.setListFile(list);
        }
        return ResponseResultApp.success(vo);
    }
}
